package io.renren.modules.task.validator;

import io.renren.modules.task.utils.ValidatorUtil;
import org.apache.commons.lang.StringUtils;
import org.hamcrest.core.Is;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/8 10:22
 * @Version 1.0
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.require(); //这个require是IsMobile哪个注解传进来的。
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(required){
            return ValidatorUtil.isMobile(value);
        }else {
            if(StringUtils.isEmpty(value)){
                return true; //允许的
            }else {
                return ValidatorUtil.isMobile(value);
            }
        }
    }
}
